﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace G.SL.Utils
{
    /// <summary>
    /// 文件内容的类型，主要是配合文件和对象互相序列化用的
    /// </summary>
    public enum FileContentType
    {
        XML,
        JSON
    }

    public static class APPFileHelp
    {
        /// <summary>
        /// 根据文件地址将文件内容转换成相应的泛型对象,如果文件不存在将会返回泛型的默认类型，比如是引用类型的，那么返回的是null
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="fileName"></param>
        /// <param name="fileType"></param>
        /// <returns></returns>
        public static T DeserializeObjectFromFile<T>(string path, FileContentType fileType = FileContentType.XML)
        {
            using (var isoFile = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication())
            {
                try
                {
                    T objTarget = default(T);
                    if (!isoFile.FileExists(path))
                        return default(T);
                    using (var file = isoFile.OpenFile(path, System.IO.FileMode.Open))
                    {
                        switch (fileType)
                        {
                            case FileContentType.XML:
                                objTarget = SerializerHelp.XMLDeserialize<T>(file);
                                break;
                            case FileContentType.JSON:
                                objTarget = SerializerHelp.JsonDeserialize<T>(file);
                                break;
                        }
                        file.Close();
                        return objTarget;
                    }
                }
                catch (System.IO.FileNotFoundException ex)
                {
                    return default(T);
                }
            }
        }


        public static object DeserializeObjectFromFile(string path, Type type, FileContentType fileType = FileContentType.XML)
        {
            using (var isoFile = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication())
            {
                try
                {
                    object objTarget = null;
                    if (!isoFile.FileExists(path))
                        return null;
                    using (var file = isoFile.OpenFile(path, System.IO.FileMode.Open))
                    {
                        switch (fileType)
                        {
                            case FileContentType.XML:
                                objTarget = SerializerHelp.XMLDeserialize(file, type);
                                break;
                            case FileContentType.JSON:
                                objTarget = SerializerHelp.JsonDeserialize(file, type);
                                break;
                        }
                        file.Close();
                        return objTarget;
                    }
                }
                catch (System.IO.FileNotFoundException ex)
                {
                    return null;
                }
            }
        }



        public static void SerializeObjectToFile(string path, object target, FileContentType fileType = FileContentType.XML)
        {
            using (var isoFile = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (var file = isoFile.CreateFile(path))
                {
                    switch (fileType)
                    {
                        case FileContentType.XML:
                            SerializerHelp.XmlSerializer(target, file);
                            break;
                        case FileContentType.JSON:
                            SerializerHelp.JsonSerializer(target, file);
                            break;
                        default:
                            throw new ArgumentException("不支持该类型的FileContentType");
                    }
                    file.Close();
                }
            }
        }
    }
}
